(define (make-mobile left right)
    (cons left right)
)
(define (make-branch length structure)
    (cons length structure)
)
(define (left-branch mobile)
    (car mobile)
)
(define (right-branch mobile)
    (cdr mobile)
)

(define (branch-length branch)
    (car branch)
)
(define (branch-structure branch)
    (cdr branch)
)
; 只需要改动构造函数和选择函数，
(define (total-weight mobile)
    (define (weight-branch branch)
        (let (
                (structure (branch-structure branch))
                (length (branch-length branch)) 
            )

            (if (pair? structure)
                (* (total-weight structure) length)
                (* structure length)
            )
        )
    )
    (+ 
        (weight-branch
            (left-branch mobile)
        )
        (weight-branch
            (right-branch mobile)
        )
    )
)
(define mobile-x 
    (make-mobile 
        (make-branch 10 10)
        (make-branch 
            1
            (make-mobile 
                (make-branch 5 10)
                (make-branch 5 10)
            ) 
        )
    )
)
(display (total-weight mobile-x))
(newline)


(define (weight-branch2 branch)
    (let (
            (structure (branch-structure branch))
            (length (branch-length branch)) 
        )
        (if (pair? structure)
            (* (total-weight structure) length)
            (* structure length)
        )
    )
)

(define (balance? mobile)
    (let (
            (lb (left-branch mobile))
            (rb (right-branch mobile))
        ) 
        (and 
            (= (weight-branch2 lb)
                (weight-branch2 rb)
            )
            (if (list? (branch-structure lb))
                (balance? (branch-structure lb))
                #t
            )
            (if (list? (branch-structure rb))
                (balance? (branch-structure rb))
                #t
            )               
        )
    )
)
(display (balance? mobile-x))
(exit)